package com.sdehua.wear.service.impl;

import com.sdehua.wear.entity.Goods;
import com.sdehua.wear.dao.GoodsDao;
import com.sdehua.wear.service.GoodsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Goods)表服务实现类
 *
 * @author makejava
 * @since 2021-12-31 15:28:48
 */
@Service("goodsService")
@Log4j2
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Goods queryById(Integer id) {
        return this.goodsDao.queryById(id);
    }

    @Override
    public List<Goods> queryAll() {
        return this.goodsDao.queryAll(null);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Goods> queryAllByLimit(int offset, int limit) {
        return this.goodsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param goods 实例对象
     * @return 实例对象
     */
    @Override
    public Goods insert(Goods goods) {
        this.goodsDao.insert(goods);
        return goods;
    }

    /**
     * 修改数据
     *
     * @param goods 实例对象
     * @return 实例对象
     */
    @Override
    public Goods update(Goods goods) {
        this.goodsDao.update(goods);
        return this.queryById(goods.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.goodsDao.deleteById(id) > 0;
    }

    /**
     * @MethodName queryByTypeId 
     * @Author coffee
     * @Description 通过类型的id来查找商品
     * @Date 16:58 2021/12/31 
     * @Param [id]      
     * @return java.util.List<com.sdehua.wear.entity.Goods>
     **/
    @Override
    public List<Goods> queryByTypeId(int id) {
        return this.goodsDao.queryByTypeId(id);
    }

    @Override
    public List<Goods> queryByCondition(String typeId,String sizeConditions, String materialConditions, String styleConditions) {
        String[] sizeArr = null;
        String[] materialArr = null;
        String[] styleArr = null;
        if (!Objects.equals(sizeConditions,null) && !sizeConditions.equals("")){
            sizeArr = sizeConditions.split(",");
            if (sizeArr.length < 1)
                sizeArr=null;
        }
        if (!Objects.equals(materialConditions,null) && !materialConditions.equals("")){
            materialArr = materialConditions.split(",");
            if (materialArr.length < 1)
                materialArr=null;
        }
        if (!Objects.equals(styleConditions,null) && !styleConditions.equals("")){
            styleArr = styleConditions.split(",");
            if (styleArr.length < 1)
                styleArr=null;
        }
        List<Goods> list = goodsDao.queryByCondition(typeId,sizeArr,materialArr,styleArr);
        log.info(sizeArr);
        log.info(materialArr);
        log.info(styleArr);
        log.info("=========INFO=========");
        return list;
    }
}